package net.ib.mtalk.ui.setting.user;

import java.util.UUID;

import net.ib.mntalk.protocol.Common;
import net.ib.mntalk.protocol.Common.AccessToken;
import net.ib.mntalk.protocol.Common.AccountAuthToken;
import net.ib.mntalk.protocol.Common.AccountId;
import net.ib.mntalk.protocol.Common.AccountInfo;
import net.ib.mntalk.protocol.Common.AccountToken;
import net.ib.mntalk.protocol.Common.Calendar;
import net.ib.mntalk.protocol.Common.FileKey;
import net.ib.mntalk.protocol.Common.Name;
import net.ib.mntalk.protocol.Common.PhoneAccountInfo;
import net.ib.mntalk.protocol.Common.UUIDMsg;
import net.ib.mntalk.protocol.Common.UUIDSlicePredicate;
import net.ib.mntalk.protocol.Common.UUIDSliceRange;
import net.ib.mntalk.protocol.Common.UserProfile;
import net.ib.mntalk.protocol.Session.AcquireAccountAuthTokenReq;
import net.ib.mntalk.protocol.Session.AcquireAccountTokenReq;
import net.ib.mntalk.protocol.Session.GetBuddyInfosReq;
import net.ib.mntalk.protocol.Session.GetUserAccountsReq;
import net.ib.mntalk.protocol.Session.GetUserProfileReq;
import net.ib.mntalk.protocol.Session.RegisterAccountReq;
import net.ib.mntalk.protocol.Session.SetUserProfileReq;
import net.ib.mntalk.protocol.Session.UnRegisterAccountReq;
import net.ib.mtalk.constant.MTalkConstant;
import net.ib.mtalk.global.GlobalPreferences;
import net.ib.mtalk.network.task.AcquireAccountAuthTask;
import net.ib.mtalk.network.task.AcquireAccountTask;
import net.ib.mtalk.network.task.GetBuddyInfosTask;
import net.ib.mtalk.network.task.GetUserAccountsTask;
import net.ib.mtalk.network.task.GetUserProfilesTask;
import net.ib.mtalk.network.task.RegisterAccountTask;
import net.ib.mtalk.network.task.SetUserProfileTask;
import net.ib.mtalk.network.task.UnRegisterAccountTask;
import net.ib.mtalk.ui.UIController;
import net.ib.mtalk.util.GpbSupport;
import net.ib.mtalk.util.MTalkLogWriter;
import android.content.Context;
import android.os.Handler;
import android.util.Log;

public class UserController extends UIController{

	public UserController(Context context, Handler handler) {
		super(context, handler);
		mUiHandler = handler;
	}
	
	public void setUserProfile(String userName, String userStatus, int y, int m, int d){
		try{
			MTalkLogWriter.d("UserController.setUserProfile()");
			
			SetUserProfileReq setUserProfileReq = new SetUserProfileReq();
			Name name = new Common.Name().setFirstName(userName).setLastName("").setMiddleName("")
							.setNamePrefix("").setNameSuffix("")
							.setPhoneticFamilyName("").setPhoneticGivenName("").setPhoneticMiddleName("");
			
			UserProfile userProfile = new Common.UserProfile().setName(name).setStatus(userStatus);
			setUserProfileReq.setProfile(userProfile);
			setUserProfileReq.addProperty(Common.USER_PROPERTY_NAME);
			setUserProfileReq.addProperty(Common.USER_PROPERTY_STATUS);
			
			//if(y > 0 && m > 0 && d > 0)
			{
				Calendar c = new Calendar().setYear(y).setMonth(m).setDay(d).setCalendar(3);
				userProfile.setBirthday(c);
				setUserProfileReq.addProperty(Common.USER_PROPERTY_BIRTHDAY);
			}
			
			SetUserProfileTask task = new SetUserProfileTask(mContext, mUiHandler, setUserProfileReq);
			task.execute();
		} catch (Exception e) {
			Log.e(MTalkConstant.TAG, "setUserProfile", e);
		}
	}
	
	public void setUserProfile(FileKey fileKey)
	{
		SetUserProfileReq setUserProfileReq = new SetUserProfileReq();
		
		UserProfile userProfile = new Common.UserProfile().setProfileImage(fileKey);
		setUserProfileReq.setProfile(userProfile);
		setUserProfileReq.addProperty(Common.USER_PROPERTY_PROFILE_IMAGE);
		
		SetUserProfileTask task = new SetUserProfileTask(mContext, mUiHandler, setUserProfileReq);
		task.execute();
	}
	
	public void setUserAccount(AccountToken accountToken){
		try{
			MTalkLogWriter.d("UserController.setUserAccount()");
			
			RegisterAccountReq registerAccountReq = new RegisterAccountReq();
			registerAccountReq.setAccountToken(accountToken);
			registerAccountReq.setAccessToken(GlobalPreferences.getMessageFromPreferences(AccessToken.class));
			registerAccountReq.setUserUuid(GlobalPreferences.getUserUUID());
			
			RegisterAccountTask task = new RegisterAccountTask(mContext, mUiHandler, registerAccountReq);
			task.execute();
		} catch (Exception e) {
			Log.e(MTalkConstant.TAG, "setUserProfile", e);
		}
	}
	
	/**
	 * Buddy Info 조회
	 * @param GetBuddyInfosReq
	 */
	public void getBuddyInfos(GetBuddyInfosReq getBuddyInfosReq){
		try {
			MTalkLogWriter.d("UserController.getBuddyInfos()");
			GetBuddyInfosTask task = new GetBuddyInfosTask(mContext, mUiHandler, getBuddyInfosReq);
			task.execute();
		} catch (Exception e) {
			Log.e(MTalkConstant.TAG, "addContacts", e);
		}
	}
	
	/**
	 * getUserProfile 얻기
	 * @param GetUserProfileReq
	 */
	public void getUserProfile(){
		try {
			MTalkLogWriter.d("UserController.getUserProfile()");
			GetUserProfileReq getUserProfileReq = new GetUserProfileReq();
			
			getUserProfileReq.setUserUuid(GlobalPreferences.getUserUUID());
			getUserProfileReq.addProperty(Common.USER_PROPERTY_NAME);
			getUserProfileReq.addProperty(Common.USER_PROPERTY_STATUS);
			getUserProfileReq.addProperty(Common.USER_PROPERTY_BIRTHDAY);
			getUserProfileReq.addProperty(Common.USER_PROPERTY_PROFILE_IMAGE);
			
			GetUserProfilesTask task = new GetUserProfilesTask(mContext, mUiHandler, getUserProfileReq);
			task.execute();
		} catch (Exception e) {
			Log.e(MTalkConstant.TAG, "getUserProfile", e);
		}
	}
	
	public void getUserAccount(){
		try {
			MTalkLogWriter.d("UserController.getUserAccount()");
			UUIDSlicePredicate predicate = new UUIDSlicePredicate();
			UUIDSliceRange slice_range = new UUIDSliceRange();
			slice_range.setAscending(true);
			slice_range.setMaxCount(UserActivity.GET_ACCOUNTS_COUNT);
			GetUserAccountsReq getUserAccountReq = new GetUserAccountsReq();
			predicate.setSliceRange(slice_range);
			getUserAccountReq.setPredicate(predicate);
			
			GetUserAccountsTask task = new GetUserAccountsTask(mContext, mUiHandler, getUserAccountReq);
			task.execute();
		} catch (Exception e) {
			Log.e(MTalkConstant.TAG, "getUserAccount", e);
		}
	}
	
	public void acquireAccountAuthToServer(String countryCode, String cellNumber) {
		AcquireAccountAuthTokenReq acquireAccountAuthTokenReq = new AcquireAccountAuthTokenReq();
		
		UUIDMsg appUuId = GpbSupport.makeUuidMsg(UUID.fromString("6fca4926-7558-4b09-ab05-6bb51bfd1eef"));
		
		String appSecret = "abcdefghijklmnopqrstuvwxyz";
		
		AccountInfo info = new AccountInfo();
		PhoneAccountInfo cellInfo = new PhoneAccountInfo();
		cellInfo.setCountryCode(countryCode);
		cellInfo.setPhoneNumber(cellNumber);
		info.setPhone(cellInfo);
		AccountId accountId = new AccountId();
		accountId.setType(Common.Phone);
		//accountId.setIdentifier(cellNumber);	// 임시 테스트용 아래 코드가 정석. 협의중
		accountId.setIdentifier(countryCode + " " + cellNumber);
		info.setId(accountId);	//ljw 0412
		
		acquireAccountAuthTokenReq.setAppUuid(appUuId);
		acquireAccountAuthTokenReq.setAppSecret(appSecret);
		acquireAccountAuthTokenReq.setAccountInfo(info);
		acquireAccountAuthTokenReq.setAuthcodeDeliverytype(Common.SELF_SMS);
		
		AcquireAccountAuthTask task = new AcquireAccountAuthTask(mContext, mUiHandler, acquireAccountAuthTokenReq);
		task.execute();
	}
	
	public void acquireAccountToServer(AccountAuthToken accountAuthToken, String authCode) {
		AcquireAccountTokenReq aquireAccountTokenReq = new AcquireAccountTokenReq();
		aquireAccountTokenReq.setAccountAuthToken(accountAuthToken);
		aquireAccountTokenReq.setAuthCode(authCode);
		
		AcquireAccountTask task = new AcquireAccountTask(mContext, mUiHandler, aquireAccountTokenReq);
		task.execute();
	}
	
	public void unRegisterAccount(String id, int type)
	{
		UnRegisterAccountReq unRegisterAccountReq = new UnRegisterAccountReq();
		
		AccountId accountId = new AccountId();
		accountId.setType(type);
		accountId.setIdentifier(id);
		
		unRegisterAccountReq.setAccessToken(GlobalPreferences.getMessageFromPreferences(AccessToken.class));
		unRegisterAccountReq.setAccountId(accountId);
		unRegisterAccountReq.setUserUuid(GlobalPreferences.getUserUUID());
		
		UnRegisterAccountTask task = new UnRegisterAccountTask(mContext, mUiHandler, unRegisterAccountReq);
		task.execute();
	}
}
